<?php
	class Admin_menu extends My_Controller
	{
		function Admin_menu()
		{
			parent::__construct();
			$this->check_login();
			$this->permiss_admin();
		}
		
		function index()
		{
			$_SESSION['select_id']		= NULL;
			$_SESSION['name']			= NULL;
			$data['title'] 			= "Quản Lý Menu Admin";
			$this->load->library('pagination');
			$per_page 				= 30;		
			$total 	  				= $this->mod_lib->count('admin_menu', array());
			$data['menu_admin'] 	= $this->mod_lib->load_all('admin_menu', '', array(), $per_page, $this->uri->segment(4), array("cat_id"=>"ASC", 'order'=>'ASC', 'name'=>'ASC'));										
			$base_url				= site_url('admins/admin_menu/index/');		
			$config['base_url']		= $base_url;
			$config['per_page']		= $per_page;
			$config['total_rows']	= $total;		
			$config['uri_segment']	= 4;
			$data['rs']				= $this->uri->segment(4);	
			$this->pagination->initialize($config);		
			$data['paging']			= $this->pagination->create_links();
			$data['content_admin']	= 'admins/admin_menu/index';
			$this->load->view('admins/template_admin', $data);
		}
		
		function search()
		{
			if ($this->input->post('cmd'))
			{
				$like		= array();
				$where		= array();
				$select_id				= $this->input->post('select_id');
				$name					= $this->input->post('name');
				
				if (!empty($select_id))
				{					
					if (!empty($name))
					{
						if ($select_id	== 1)
						{
							$where			= $this->check_search($where, $name, 'admin_menu.id');						
						}
						else if ($select_id	== 2)
						{
							$like			= $this->check_search($like, $name, 'admin_menu.name');
						}
						else if ($select_id	== 3)
						{
							$like			= $this->check_search($like, $name, 'admin_menu.link');
						}
					}					
				}
				$_SESSION['search']			= array('where'=>$where, 'like'=>$like);
				$_SESSION['select_id']		= $select_id;
				$_SESSION['name']			= $name;
			}
			$data['title'] 				= "Tìm kiếm";
			$this->load->library('pagination');
			$per_page 					= 30;
			$uri						= 4;
			$total 	  					= $this->mod_lib->count_search_join('admin_menu', $_SESSION['search']['like'], $_SESSION['search']['where'], array());				
			$data['menu_admin']			= $this->mod_lib->load_all_search_join('admin_menu', '', $_SESSION['search']['where'], $_SESSION['search']['like'], array(), $per_page, $this->uri->segment($uri), array("cat_id"=>"ASC", 'order'=>'ASC', 'name'=>'ASC'));
			$base_url					= site_url('admins/admin_menu/search');
			$config['base_url']			= $base_url;
			$config['per_page']			= $per_page;
			$config['total_rows']		= $total;
			$config['uri_segment']		= $uri;
			$data['rs']					= $this->uri->segment($uri);									
			$data['uri']				= $this->uri->uri_string();
			$this->pagination->initialize($config); 
			$data['paging']				= $this->pagination->create_links();
			$data['content_admin']	= 'admins/admin_menu/index';
			$this->load->view('admins/template_admin', $data);
		}
		
		function add()
		{
			$i = $this->permiss_admin_add();				
			if(isset($i['all']) || isset($i['add']))
			{
				$data['title'] 		= "Thêm Mới Menu Admin";
				if ($this->input->post('cmd'))
				{
					$this->db->select_max('order', 'category_order');
					$query 	= $this->db->get('admin_menu')->row_array();
					$order	= $query['category_order'] + 1;
					
					$controller		= trim($this->input->post('link'));
					$controller		= explode("/", $controller);
					$controller		= $controller[0];
					$cat_id   		= $this->input->post('cat_id');
					if ($cat_id		== 0)
					{
						$result			= array(
												'cat_id'   		=> $this->input->post('cat_id'),
												'name' 			=> trim($this->input->post('name')),
												'link' 			=> trim($this->input->post('link')),
												'controller' 	=> $controller,
												'order'			=> $order,
												'status'   		=> $this->input->post('status')
						);
					}
					else
					{
						$result			= array(
												'cat_id'   		=> $this->input->post('cat_id'),
												'name' 			=> trim($this->input->post('name')),
												'link' 			=> trim($this->input->post('link')),
												'controller' 	=> $controller,
												'status'   		=> $this->input->post('status')
						);
					}
					$a 	= $this->db->get_where('admin_menu', array('name'=>$this->input->post('name'), 'cat_id'=>$this->input->post('cat_id')));
					if ($a->num_rows() > 0)
					{
						$_SESSION['error_add']	= 'Tên Menu Admin này đã tồn tại. Vui lòng chọn Tên Menu Admin khác';
						redirect(base_url().'admins/admin_menu/add');
					}
					else 
					{
						$_SESSION['error_add']	= '';
						$this->db->insert('admin_menu', $result);
						$data1 			= $this->mod_lib->load_all('admin_menu', '', array(), '1', '', array("id"=>"desc"));		
						$menu_id		= $data1[0]['id'];
						$admin 			= $this->mod_lib->load_all('admins','',array(),'','',array("id"=>"desc"));
						$group_admin 	= $this->mod_lib->load_all('admin_groups', '', array(), '', '', array("id"=>"desc"));
						foreach ($group_admin as $gra)
						{
							$groups_admin_id	= $gra['id'];
							$data3	= array(
										'menu_admin_id' 			=> $menu_id,
										'access' 					=> 0,
										'add'   					=> 0,
										'edit'   					=> 0,
										'del'						=> 0,
										'group_admin_id'   			=> $groups_admin_id
							);		
							$this->db->insert('admin_permiss', $data3);				
						}
						$action	= 'Thêm mới Menu Admin: '.$this->input->post('name');
						$this->mod_lib->insert_log($action, $_SESSION['admin_id']);
						if ($this->input->post('cmd')	== 'Add')
						{
							echo '<script>alert("Bạn đã thêm mới thành công một menu admin."); window.location = "'.base_url().'admins/admin_menu"</script>';
						}
						else if ($this->input->post('cmd')	== 'Save & Add')
						{
							redirect(base_url().'admins/admin_menu/add');
						}						
					}					
				}
				$data['menu_admin']		= $this->mod_lib->load_all('admin_menu', '', array('cat_id'=>0), '', '', array('name'=>'ASC'));
				$data['content_admin']	= 'admins/admin_menu/add';
				$this->load->view('admins/template_admin', $data);
			}
			else
			{
				$_SESSION['permiss_error'] = 'Bạn không có quyền thêm menu admin';
				redirect(base_url().'admins/admin_menu');	
			}
		}
		
		function edit($id = "")
		{
			$i = $this->permiss_admin_add();
			if(isset($i['all']) || isset($i['edit']))
			{	
				$data['title']	 			= 'Sửa Menu Admin';
				$data['admin_menu_edit']	= $this->mod_lib->detail('admin_menu', array("id"=>$id));
				$data['menu_admin']			= $this->mod_lib->load_all('admin_menu', '', array('cat_id'=>0), '', '', array('name'=>'ASC'));
				$dat						= $this->mod_lib->detail('admin_menu', array("id"=>$id));
				$name						= $dat[0]['name'];
				if ($this->input->post('cmd'))
				{
					$controller		= trim($this->input->post('link'));
					$controller		= explode("/", $controller);
					$controller		= $controller[0];
					$result			= array(
											'cat_id'   		=> $this->input->post('cat_id'),
											'name' 			=> trim($this->input->post('name')),
											'link' 			=> trim($this->input->post('link')),
											'controller' 	=> $controller,
											'status'   		=> $this->input->post('status')
										);
					$this->mod_lib->update('admin_menu', array("id"=>$id), $result);
					$action	= "Sửa Menu Admin: ".$name.' thành: '.$this->input->post('name');
					$this->mod_lib->insert_log($action, $_SESSION['admin_id']);
					redirect($_SESSION['last_url_admin']);
				}			
				$data['content_admin']	= 'admins/admin_menu/edit';
				$this->load->view('admins/template_admin', $data);
			}
			else
			{
				$_SESSION['permiss_error'] = 'Bạn không có quyền sửa menu admin';
				redirect($_SESSION['last_url_admin']);
			}
		}
		
		
		function edit_order($id, $order)
		{
			$i = $this->permiss_admin_add();				
			if(isset($i['all']) || isset($i['edit']))
			{
				$results = $this->db->get_where('admin_menu', array('order'=>$order))->result_array();
				if($results == NULL)
				{
					$this->mod_lib->update('admin_menu', array('id'=>$id), array('order'=>$order));
				}
				else
				{
					$query2 = "UPDATE `hba_admin_menu` SET `order` = `order` + 1 WHERE `order` >= $order";												
					$this->db->query($query2);								
					$this->mod_lib->update('admin_menu', array('id'=>$id), array('order'=>$order));
				}							
				redirect($_SESSION['last_url_admin']);
			}
			else
			{			
				$_SESSION['permiss_error'] = 'Bạn không có quyền sửa';
				redirect($_SESSION['last_url_admin']);			
			}
		}
		
		function delete($id="")
		{
			$i = $this->permiss_admin_add();			
			if(isset($i['all']) || isset($i['del']))
			{
				if (empty($id))
				{
					$id			= $this->input->post('chon');
					$id			= substr($id, 0, strlen($id)-1);
					$mang_id	= explode(',', $id);
					$action     = array();
					foreach ($mang_id as $id)
					{
						$data = $this->mod_lib->detail('admin_menu',array("id"=>$id));
						$action[] = $data[0]['name'];
					}
					$hd = '';
					foreach ($action as $ac)
					{
						if ($hd	== "")
						{
							$hd .= 'Xóa Menu Admin: '.$ac;
						}
						else
						{
							$hd .= ', '.$ac;
						}					
					}
					$this->mod_lib->delete('admin_menu', $mang_id, 'id', true);
					$this->mod_lib->insert_log($hd, $_SESSION['admin_id']);
				}
				else
				{
					$dat 	= $this->mod_lib->detail('admin_menu', array('id'=>$id));
					$name 	= $dat[0]['name'];
					$action = 'Xóa menu admin: '.$name;
					$this->mod_lib->delete('admin_menu', $id, 'id');
					$this->mod_lib->insert_log($action, $_SESSION['admin_id']);
				}
				redirect($_SESSION['last_url_admin']);
			}
			else
			{
				$_SESSION['permiss_error'] = 'Bạn không có quyền xóa';
				redirect($_SESSION['last_url_admin']);
			}
		}
		
		function active($id, $status)
		{
			$i = $this->permiss_admin_add();
			if(isset($i['all']) || isset($i['edit']))
			{	
				if ($status == 1)
				{
					$update_status = 0;
				}else{
					$update_status = 1;
				}
				$data 	= array('status'=>$update_status);			
				$this->mod_lib->update('admin_menu', array("id"=>$id), $data);
				$query	= $this->db->get_where('admin_menu', array('id'=>$id))->row_array();
				$name	= $query['name'];
				$action	= 'Thay đổi trạng thái Menu Admin: '.$name;
				$this->mod_lib->insert_log($action, $_SESSION['admin_id']);
				redirect($_SESSION['last_url_admin']);
			}
			else
			{
				$_SESSION['permiss_error'] = 'Bạn không có quyền thay đổi trạng thái menu admin';
				redirect($_SESSION['last_url_admin']);
			}
		}
		
		function del($id)
		{
			$i = $this->permiss_admin_add();			
			if(isset($i['all']) || isset($i['del']))
			{
				$this->mod_lib->delete('admin_menu', $id, 'id');
				redirect($_SESSION['last_url_admin']);
			}
			else
			{
				$_SESSION['permiss_error'] = 'Bạn không có quyền xóa menu admin';
				redirect($_SESSION['last_url_admin']);
			}
		}
	}